<?php

apf_require_class('Oa_Job_Attendance_Abstract');
apf_require_class("Tools_Tools");
apf_require_class("Bll_Attendance_AccessDayRecord");
apf_require_class('Const_LeaveType');
class Oa_Job_Attendance_SetOldAbsend extends Oa_Job_Attendance_Abstract {
    /** 考勤系统读 **/
    private $readdb = "attendance_slave";
    /** 考勤系统写 **/
    private $writedb = "attendance_master";

    public function run(){
        //公出
        $start = 0;
        while (true) {
            $sql_gc = "SELECT * FROM leave_record WHERE type='公出' AND id>$start order by id asc limit 100";
            $datalist=$this->get_rows($sql_gc,array(),false,$this->writedb);
            if(empty($datalist)) {
                break;
            }
            foreach ($datalist as $val) {
                $update = array(
                        'start_time'=>str_replace(array('点','半'),array(':00'),$val['start_time']),
                        'end_time'=>str_replace(array('点','半'),array(':00'),$val['end_time']),
                );
                $upsql="UPDATE leave_record SET `start_time` =  '".$update['start_time']."',
                `end_time` =  '".$update['end_time']."' WHERE id =".$val['id'];
                //echo $upsql."\n";
                $this->execute($upsql,array(),$this->writedb);
            }
            $num = count($datalist);
            $start = $datalist[$num-1]['id'];
        }
        //exit;
        $start=0;
        $i=0;
        while (true){
            $sql_qq = "SELECT * FROM leave_record WHERE type='缺勤' AND id>$start order by id asc limit 100";
            $datalist=$this->get_rows($sql_qq,array(),false,$this->writedb);
            if(empty($datalist)) {
                break;
            }
            foreach ($datalist as $val) {
                $day = str_replace('-','',substr($val['start_time'],0,10));
                $accsql = "SELECT * FROM access_day_record WHERE user_id='".$val['user_id']."' AND access_date='".$day."'";
                $ac = $this->get_rows($accsql,array(),true,$this->writedb);
                $start_time = substr($val['start_time'],0,10);
                $end_time = substr($val['start_time'],0,10);
                if(empty($ac)){
                    $update = array(
                            'start_time'=>$start_time.' 09:00',
                            'end_time'=>$end_time.' 18:00',
                    );
                }else{
                    $access_bll =  new Bll_Attendance_AccessDayRecord();
                    $status=$access_bll->check_day_work_status($day,$ac['start_work'],$ac['end_work']);
                    $update = array();
                    if($status[0]==3 && $status[1]==4){
                        $update = array(
                                'start_time'=>$start_time.' 09:00',
                                'end_time'=>$end_time.' 18:00',
                        );
                    }elseif($status[0]==5 && $status[1]==6){
                        $update = array(
                                'start_time'=>$start_time.' 09:00',
                                'end_time'=>$end_time.' 18:00',
                        );
                    }else{
                        if($status[0]!=5){
                            $update = array(
                                    'start_time'=>$start_time.' 09:00',
                                    'end_time'=>$end_time.' 14:00',
                            );
                        }elseif($status[1]!=6){
                            $update = array(
                                    'start_time'=>$start_time.' 14:00',
                                    'end_time'=>$end_time.' 18:00',
                            );
                        }
                    }
                }
                $upsql="UPDATE leave_record SET `start_time` =  '".$update['start_time']."',`end_time` =  '".$update['end_time']."' WHERE id =".$val['id'];
                $this->execute($upsql,array(),$this->writedb);
                $i++;
            }
            $num = count($datalist);
            $start = $datalist[$num-1]['id'];
        }
    }
}